﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using Microsoft.ApplicationBlocks.Data;
using System.Data;

namespace Gama.Service
{
    public static  class Extention
    {
        public static IList<T> ToList<T>(this ArrayList helper)
        {
            return helper.Cast<T>().ToList();
        }
    
        public static ArrayList ToArray<T>(this IList<T> helper)
        {
            var arrList = new ArrayList();            
            foreach (T item in helper)
            {
                arrList.Add(item);
            }
            return arrList;
        }
        public static T ConvertToDto<T>(this IDataReader helper)
        {
            return (T)Reflector.FillObject(helper, typeof(T), true); ;
        }
        public static IList<T> ToList<T>(this IDataReader helper)
        {
            var array = Reflector.FillCollection(helper, typeof(T), true);
            return array.Cast<T>().ToList();
        }
        #region "IDataReader"
        public static ArrayList ToArray<T>(this IDataReader helper)
        {
            return Reflector.FillCollection(helper, typeof(T), true);
        }

        public static DataTable ToDataTable(this IDataReader helper)
        {
            var objDataTable = new DataTable();
            int intFieldCount = helper.FieldCount;
            int intCounter;
            for (intCounter = 0; intCounter < intFieldCount - 1; intCounter++)
            {
                objDataTable.Columns.Add(helper.GetName(intCounter), helper.GetFieldType(intCounter));
            }
            objDataTable.BeginLoadData();
            Object[] objValues = new Object[intFieldCount - 1];
            while (helper.Read())
            {
                helper.GetValues(objValues);
                objDataTable.LoadDataRow(objValues, true);
            }
            helper.Close();
            objDataTable.EndLoadData();

            return objDataTable;
        }
        #endregion
        public static bool ToBoolean(this object helper)
        {
            return Convert.ToBoolean(helper);
        }
        #region "String"
        public static string Truncate(this string helper, int length)
        {
            return helper.Length > length ? string.Format("{0}...", helper.Substring(0, length)) : helper;
        }

        public static string CovertToCharacter(this string helper)
        {
            if (string.IsNullOrEmpty(helper))
                return string.Empty;
            var vRegRegex = new System.Text.RegularExpressions.Regex("\\p{IsCombiningDiacriticalMarks}+");
            var vStrFormD = helper.Normalize(NormalizationForm.FormD);
            return vRegRegex.Replace(vStrFormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D').Replace(" ", "-").ToLower();
        }

        public static string ToMoneyVn(this object helper)
        {
            return string.Format("{0:0,0} VND", helper);
        }
        #endregion       
    }
}
